﻿
package com.android.finaltracer.channel;

import android.content.Context;

import com.android.finaltracer.channel.Channel.ChannelEventCallback;
import com.android.finaltracer.engine.EventNotify;
import com.android.finaltracer.model.DataFormatBasic;
import com.android.finaltracer.model.DataFormatInterface;

public class ChannelProxy implements Communicate, ChannelEventCallback {
    public static final int CHANNEL_SMS = 0;
    public static final int CHANNEL_HTTP = CHANNEL_SMS + 1;
    public static final int CHANNEL_COUNT = CHANNEL_HTTP + 1;
    private static ChannelProxy instance;
    private EventNotify mService;
    private Channel[] mChannels = new Channel[CHANNEL_COUNT];
    private int mChannelType = CHANNEL_SMS;

    // 应该还需要实例化SMS并将这个notify传下去
    // 需要实例化所有channel
    private ChannelProxy(Context context, EventNotify service) {
        this.mService = service;
        mChannels[CHANNEL_SMS] = new Sms(context, this);
        mChannels[CHANNEL_HTTP] = new Http(context, this);
    }

    public static ChannelProxy getInstance(Context context,
            EventNotify mEventNotify) {
        if (instance == null) {
            instance = new ChannelProxy(context, mEventNotify);
        }
        return instance;
    }

    // public void setChannel(int channelType) {
    // this.mChannelType = channelType;
    // }

    @Override
    public void onReceive(String from, DataFormatInterface dfb) {
        if (mService != null) {
            mService.receive(from, dfb);
        }
    }

    public boolean isAlive() {
        return mChannels[CHANNEL_SMS].isAvailable()
                || mChannels[CHANNEL_HTTP].isAvailable();
    }

    @Override
    public void send(String target, DataFormatBasic dataFormatBasic) {
        // TODO Auto-generated method stub
        // if (mChannels != null) {
        // if (mChannels[mChannelType].isAvailable()) {
        // mChannels[mChannelType].send(target, dataFormatBasic);
        // }
        // }
        if (mChannelType == CHANNEL_HTTP
                && mChannels[mChannelType].isAvailable()) {
            // if(mChannels[CHANNEL_HTTP]!=null&&mChannels[CHANNEL_HTTP].isAvailable()){
            send(target, dataFormatBasic, CHANNEL_HTTP);
        } else {
            send(target, dataFormatBasic, CHANNEL_SMS);
        }
    }

    @Override
    public boolean send(String target, DataFormatBasic dataFormatBasic,
            int sendType) {
        // TODO Auto-generated method stub
        boolean sendSuccess = false;
        if (mChannels != null && mChannels[sendType] != null
                && mChannels[sendType].isAvailable()) {
            mChannels[sendType].send(target, dataFormatBasic);
            sendSuccess = true;
        }
        return sendSuccess;
    }

    @Override
    public void onNetworkStateChanged(boolean available) {

    }

    @Override
    public void onHeartbeatResult(boolean result) {

    }

    public boolean startHttpRecvPositionReport(long delayMillis) {
        boolean success = false;
        if (mChannels != null && mChannels[CHANNEL_HTTP] != null
                && mChannels[CHANNEL_HTTP].isAvailable()) {
            ((Http) mChannels[CHANNEL_HTTP])
                    .startRecvPositionReport(delayMillis);
            success = true;
        }
        return success;
    }

    public boolean stopHttpRecvPositionReport() {
        boolean success = false;
        if (mChannels != null && mChannels[CHANNEL_HTTP] != null) {
            ((Http) mChannels[CHANNEL_HTTP]).stopRecvPositionReport();
            success = true;
        }
        return success;
    }
}
